﻿using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;

public class CameraShake : MonoBehaviour
{
    private float decay;
    private float intensity;
    public bool isShaking;
    private Vector3 OriginalPos;
    private Quaternion OriginalRot;

    private void Awake()
    {
        if (base.gameObject.tag == "MainCamera")
        {
            main = this;
        }
    }

    public void DoShake(float intensity = 0.3f, float decay = 0.02f)
    {
        this.OriginalPos = base.transform.position;
        this.intensity = intensity;
        this.decay = decay;
        this.isShaking = true;
    }

    private void OnDestroy()
    {
        if (base.gameObject.tag == "MainCamera")
        {
            main = null;
        }
    }

    public static void ShakeMainCamera(float intensity = 0.3f, float decay = 0.02f)
    {
        if (main != null)
        {
            main.DoShake(intensity, decay);
        }
    }

    private void Start()
    {
        this.OriginalRot = base.transform.rotation;
        this.isShaking = false;
    }

    private void Update()
    {
        if (this.intensity > 0f)
        {
            base.transform.position = this.OriginalPos + ((Vector3) (UnityEngine.Random.insideUnitSphere * this.intensity));
            base.transform.rotation = new Quaternion(this.OriginalRot.x + (UnityEngine.Random.Range(-this.intensity, this.intensity) * 0.2f), this.OriginalRot.y + (UnityEngine.Random.Range(-this.intensity, this.intensity) * 0.2f), this.OriginalRot.z + (UnityEngine.Random.Range(-this.intensity, this.intensity) * 0.2f), this.OriginalRot.w + (UnityEngine.Random.Range(-this.intensity, this.intensity) * 0.2f));
            this.intensity -= this.decay;
        }
        else if (this.isShaking)
        {
            this.isShaking = false;
        }
    }

    private static CameraShake main
    {
        [CompilerGenerated]
        get
        {
            return <main>k__BackingField;
        }
        [CompilerGenerated]
        set
        {
            <main>k__BackingField = value;
        }
    }
}

